import {Form, Modal, Row, Col, Input, Select, InputNumber, Divider} from 'antd';
import {useEffect} from 'react';
import {connect} from 'umi';
import styles from "../styles.less";

const DataForm = (props) => {
  const {dispatch, submitting, jobGroupList} = props;
  const {dataForm, setDataForm, tablePayload, setTablePayload} = props.data;
  const [form] = Form.useForm();

  useEffect(() => {
    dispatch({
      type: 'XxlJobCommon/getJobGroups',
      payload: {},
    });
  }, [dispatch]);

  /* 初始化表单数据 */
  useEffect(() => {
    if (!dataForm || !dataForm.data) {
      form.setFieldsValue({
        triggerStatus: 0,
        scheduleType: 'CRON',
        glueType: 'BEAN',
        executorRouteStrategy: 'FIRST',
        misfireStrategy: 'DO_NOTHING',
        executorBlockStrategy: 'SERIAL_EXECUTION',
        executorTimeout: 0,
        executorFailRetryCount: 0,
      });
    }
    if (dataForm && dataForm.data) {
      form.setFieldsValue({...dataForm.data});
    }
  }, [dataForm, form]);

  /* 确定操作 */
  function onOk() {
    form.validateFields().then(values => {
      if (submitting) {
        return;
      }
      let keys = Object.keys(values);
      keys.forEach(d => {
        values[d] = values[d] === null ? undefined : values[d];
      });
      dispatch({
        type: values.id ? 'PMonitorXxlJob/edit' : 'PMonitorXxlJob/add',
        payload: values,
        callback: () => {
          onCancel();
          setTablePayload({...tablePayload});
        },
      });
    });
  }

  /* 取消操作 */
  function onCancel() {
    setDataForm(undefined);
    form.resetFields();
  }

  return (
    <Modal
      confirmLoading={submitting}
      title={dataForm ? dataForm.title : ''}
      open={dataForm ? dataForm.visible : false}
      onOk={onOk}
      onCancel={onCancel}
      forceRender={true}
      width={1200}
      className={styles.myModal}
    >
      <Form layout='horizontal' labelCol={{span: 5}} form={form}>
        {dataForm && dataForm.data && (
          <Form.Item name='id' hidden={true}>
            <Input/>
          </Form.Item>
        )}
        <Form.Item name='triggerStatus' hidden={true}>
          <Input/>
        </Form.Item>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='jobGroup' label='任务执行器'
              rules={[
                {required: true, message: '任务执行器不能为空'},
              ]}>
              <Select
                placeholder='请选择任务执行器' options={jobGroupList} allowClear showSearch
                filterOption={(input, option) => (option?.label ?? '').includes(input)}
              />
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='jobDesc' label='任务描述'
              rules={[
                {required: true, message: '任务描述不能为空'},
              ]}>
              <Input placeholder='请输入任务描述'/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='author' label='任务负责人'
              rules={[
                {required: true, message: '任务负责人不能为空'},
              ]}>
              <Input placeholder='请输入任务负责人'/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='alarmEmail' label='报警邮件'
              rules={[
                {required: false, message: '报警邮件不能为空'},
              ]}>
              <Input placeholder='请输入报警邮件，多个则用逗号分割'/>
            </Form.Item>
          </Col>
        </Row>
        <Divider style={{marginTop: 0}}/>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='scheduleType' label='调度类型'
              rules={[
                {required: true, message: '调度类型不能为空'},
              ]}
            >
              <Select placeholder='请选择调度类型'>
                <Select.Option value={'CRON'}>CRON</Select.Option>
              </Select>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='scheduleConf' label='调度配置'
              rules={[
                {required: true, message: '调度配置不能为空'},
              ]}>
              <Input placeholder='请输入调度配置'/>
            </Form.Item>
          </Col>
        </Row>
        <Divider style={{marginTop: 0}}/>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='glueType' label='运行模式'
              rules={[
                {required: true, message: '运行模式不能为空'},
              ]}
            >
              <Select placeholder='请选择运行模式'>
                <Select.Option value={'BEAN'}>BEAN</Select.Option>
              </Select>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='executorHandler' label='执行方法'
              rules={[
                {required: true, message: '执行方法不能为空'},
              ]}>
              <Input placeholder='请输入执行方法'/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='executorParam' label='任务参数'
              rules={[
                {required: false, message: '任务参数不能为空'},
              ]}>
              <Input placeholder='请输入任务参数'/>
            </Form.Item>
          </Col>
        </Row>
        <Divider style={{marginTop: 0}}/>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='executorRouteStrategy' label='路由策略'
              rules={[
                {required: true, message: '路由策略不能为空'},
              ]}
            >
              <Select placeholder='请选择路由策略'>
                <Select.Option value={'FIRST'}>第一个</Select.Option>
                <Select.Option value={'LAST'}>最后一个</Select.Option>
                <Select.Option value={'ROUND'}>轮询</Select.Option>
                <Select.Option value={'RANDOM'}>随机</Select.Option>
                <Select.Option value={'CONSISTENT_HASH'}>一致性HASH</Select.Option>
                <Select.Option value={'LEAST_FREQUENTLY_USED'}>最不经常使用</Select.Option>
                <Select.Option value={'LEAST_RECENTLY_USED'}>最近最久未使用</Select.Option>
                <Select.Option value={'FAILOVER'}>故障转移</Select.Option>
                <Select.Option value={'BUSYOVER'}>忙碌转移</Select.Option>
                <Select.Option value={'SHARDING_BROADCAST'}>分片广播</Select.Option>
              </Select>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='childJobid' label='子任务ID'
              rules={[
                {required: false, message: '请输入子任务ID'},
              ]}>
              <Input placeholder='请输入子任务ID，多个则逗号分割'/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='misfireStrategy' label='调度过期策略'
              rules={[
                {required: true, message: '调度过期策略不能为空'},
              ]}
            >
              <Select placeholder='请选择调度过期策略'>
                <Select.Option value={'DO_NOTHING'}>忽略</Select.Option>
                <Select.Option value={'FIRE_ONCE_NOW'}>立即执行一次</Select.Option>
              </Select>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='executorBlockStrategy' label='阻塞处理策略'
              rules={[
                {required: true, message: '阻塞处理策略不能为空'},
              ]}
            >
              <Select placeholder='请选择阻塞处理策略'>
                <Select.Option value={'SERIAL_EXECUTION'}>单机串行</Select.Option>
                <Select.Option value={'DISCARD_LATER'}>丢弃后续调度</Select.Option>
                <Select.Option value={'COVER_EARLY'}>覆盖之前调度</Select.Option>
              </Select>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name='executorTimeout' label='任务超时时间'
              rules={[
                {required: true, message: '任务超时时间不能为空'},
              ]}>
              <InputNumber style={{width: '100%'}} placeholder='请输入任务超时时间'/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name='executorFailRetryCount' label='失败重试次数'
              rules={[
                {required: true, message: '失败重试次数不能为空'},
              ]}>
              <InputNumber style={{width: '100%'}} placeholder='请输入失败重试次数'/>
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Modal>
  );
};


export default connect(({loading, XxlJobCommon}) => {
  return {
    submitting: loading.effects['PMonitorXxlJob/add'] || loading.effects['PMonitorXxlJob/edit'],
    jobGroupList: XxlJobCommon.jobGroupList,
  };
})(DataForm);

